package com.cskaoyan.mall.controller.admin;

import com.cskaoyan.mall.model.bo.user.UserMessageBo;
import com.cskaoyan.mall.model.vo.user.UserMessageVo;
import com.cskaoyan.mall.service.lmpl.UserServiceImpl;
import com.cskaoyan.mall.utils.UserResult;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.ServletInputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName: UserSevlet
 * @Description: TODO
 * @Auther 庄左
 * @Date 2022/6/29 21:54
 * @Version
 **/

@WebServlet("/api/admin/*")
public class UserSevlet extends HttpServlet {

    private UserServiceImpl userService = new UserServiceImpl();
    private Gson gson = new Gson();
    private UserResult result = new UserResult();

    private UserMessageBo getUserMessageBo(HttpServletRequest req) throws IOException {

        ServletInputStream inputStream = req.getInputStream();
        //写入字节数组的输出流
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        int len = 0;
        byte[] bytes = new byte[1024];
        while ((len = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, len);
        }
        //转为字符串
        String reqBody = outputStream.toString("utf-8");
        //拿到json字符串----->java对象
        return gson.fromJson(reqBody, UserMessageBo.class);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {


    }




    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI = req.getRequestURI();
        String replace = requestURI.replace(req.getContextPath() + "/api/admin/user/", "");
        if ("allUser".equals(replace)){
            searchAll(req,resp);
        }
        if ("searchUser".equals(replace)){
            searchUser(req,resp);
        }
        if("deleteUser".equals(replace)){
            deleteUser(req,resp);
        }
    }

    private void deleteUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        Integer id = Integer.valueOf(req.getParameter("id"));
        if (id==1){
            result.setCode(1000);
            result.setMessage("你不能删除admin账号");
            resp.getWriter().println(gson.toJson(result));
            return;
        }
        userService.deleteUser(id);
        result.setCode(0);
        resp.getWriter().println(gson.toJson(result));


    }

    private void searchUser(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String nickname = req.getParameter("word");
        List<UserMessageVo> user = userService.selectByName(nickname);
        result.setCode(0);
        result.setData(user);
        resp.getWriter().println(gson.toJson(result));
    }

    private void searchAll(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<UserMessageVo> users = userService.search();
        result.setCode(0);
        result.setData(users);
        resp.getWriter().println(gson.toJson(result));
    }
}
